<?php

use think\migration\Migrator;
use app\model\Agent;
use app\model\CouponConfig;

class AlterCouponConfigAddSomeThing extends Migrator
{
    public function up()
    {
        $const_value = 4;
        Agent::query()->where('status', Agent::STATUS_ACTIVE)->select()->each(function (Agent $agent) use ($const_value){
            /**
             * @var Agent $agent
             */
            $config = CouponConfig::query()->where('agent_id', $agent->id)->where('global', CouponConfig::GLOBAL_DLQJ)->find();
            if (!$config) {
                CouponConfig::create([
                    'agent_id' => $agent->id,
                    'value'    => $const_value,
                    'status'   => CouponConfig::STATUS_ACTIVE,
                    'global'   => CouponConfig::GLOBAL_DLQJ,
                ]);
            }
        });
    }

    public function down()
    {
        $const_value = 4;
        $ids = CouponConfig::where('global', CouponConfig::GLOBAL_DLQJ)
            ->where('value', $const_value)
            ->whereLike('created_at','%'.date('Y-m-d', time()).'%')->column('id');
        CouponConfig::destroy($ids);
    }
}
